package com.whq.thrift.api.web.admin.db.admin;

import java.util.Map;

import org.apache.ibatis.jdbc.SqlBuilder;

import com.whq.common.StringUtil;

public class AlertThresholdSqlProvider {
	public String getSelectRecordsSql(Map<String, Object> parameter) {
		SqlBuilder.BEGIN();
		SqlBuilder.SELECT("*");
		SqlBuilder.FROM("t_alert_threshold");

		if ((parameter.get("type") != null) && (StringUtil.isNotBlank(parameter.get("type").toString()))) {
			SqlBuilder.WHERE("type = #{type}");
		}

		if ((parameter.get("errorCode") != null) && (StringUtil.isNotBlank(parameter.get("errorCode").toString()))) {
			SqlBuilder.WHERE("error_code = #{errorCode}");
		}

		SqlBuilder.ORDER_BY("id asc");
		return SqlBuilder.SQL() + " limit #{beginIndex}, #{maxCount} ";
	}

	public String getSelectCountSql(Map<String, Object> parameter) {
		SqlBuilder.BEGIN();
		SqlBuilder.SELECT("count(*)");
		SqlBuilder.FROM("t_alert_threshold");

		if ((parameter.get("type") != null) && (StringUtil.isNotBlank(parameter.get("type").toString()))) {
			SqlBuilder.WHERE("type like #{type}");
		}

		if ((parameter.get("errorCode") != null) && (StringUtil.isNotBlank(parameter.get("errorCode").toString()))) {
			SqlBuilder.WHERE("error_code = #{errorCode}");
		}

		return SqlBuilder.SQL();
	}

	public String getUpdateSql(AlertThreshold record) {
		SqlBuilder.BEGIN();
		SqlBuilder.UPDATE("t_alert_threshold");

		SqlBuilder.SET("type = #{type}");

		SqlBuilder.SET("error_code = #{errorCode}");

		SqlBuilder.SET("min_alert_threshold = #{minAlertThreshold}");

		if (record.getUpdateTime() != null) {
			SqlBuilder.SET("update_time = #{updateTime}");
		}

		SqlBuilder.WHERE("id=#{id}");
		return SqlBuilder.SQL();
	}
	
	public String findAlertThresholdSql(Map<String , Object> parameter){
		SqlBuilder.BEGIN();
		SqlBuilder.SELECT("*");
		SqlBuilder.FROM("t_alert_threshold");

		if ((parameter.get("type") != null) && (StringUtil.isNotBlank(parameter.get("type").toString()))) {
			SqlBuilder.WHERE("type = #{type}");
		}

		if ((parameter.get("errorCode") != null) && (StringUtil.isNotBlank(parameter.get("errorCode").toString()))) {
			SqlBuilder.WHERE("error_code = #{errorCode}");
		}

		return SqlBuilder.SQL();
	}

}
